package com.gc.web.controller.business;

import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.KnowledgeClassify;
import com.gc.business.service.IKnowledgeClassifyService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.DateUtils;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 知识库类型 控制层
 *
 * @author liujinteng
 * @date 2020-10-27
 */
@Slf4j
@Controller
@RequestMapping("/business/knowledgeClassify")
public class KnowledgeClassifyController extends BaseController {
    private String prefix = "business/knowledgeClassify";

    @Autowired
    private ISysUserService userService;

    private final IKnowledgeClassifyService knowledgeClassifyService;

    @Autowired
    public KnowledgeClassifyController(IKnowledgeClassifyService knowledgeClassifyService) {
        this.knowledgeClassifyService = knowledgeClassifyService;
    }

    /**
     * 列表页
     */
    @RequiresPermissions("business:knowledgeClassify:view")
    @GetMapping
    public String knowledgeClassify() {
        return prefix + "/knowledgeClassify";
    }

    /**
     * 新增页
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit/{classifyId}")
    public String edit(@PathVariable("classifyId") Long classifyId, ModelMap mmap) {
        KnowledgeClassify knowledgeClassify = knowledgeClassifyService.getById(classifyId);
        mmap.put("knowledgeClassify", knowledgeClassify);
        return prefix + "/edit";
    }

    /**
     * 查询知识库类型列表
     */
    @RequiresPermissions("business:knowledgeClassify:list")
    @PostMapping("/list")
    @ResponseBody
    public Result list(KnowledgeClassify knowledgeClassify) {
        Long attrId = getAttrId();
        Boolean ifCompany = userService.ifCompanyRole(attrId);
        if(!ifCompany){
            return Result.success(getDataTable(new ArrayList<KnowledgeClassify>()));
        }
        startPage();
        knowledgeClassify.setAttrId(attrId);
        List<KnowledgeClassify> list = knowledgeClassifyService.list(knowledgeClassify);
        return Result.success(getDataTable(list));
    }

    /**
     * 新增保存知识库类型
     */
    @RequiresPermissions("business:knowledgeClassify:add")
    @Log(title = "知识库类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Result addSave(KnowledgeClassify knowledgeClassify) {
        String userName = ShiroUtils.getSysUser().getUserName();
        knowledgeClassify.setCreateBy(userName);
        knowledgeClassify.setAttrId(getAttrId());
        knowledgeClassify.setCreateTime(DateUtil.date());
        knowledgeClassifyService.save(knowledgeClassify);
        return Result.success();
    }

    /**
     * 修改保存知识库类型
     */
    @RequiresPermissions("business:knowledgeClassify:edit")
    @Log(title = "知识库类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(KnowledgeClassify knowledgeClassify) {
        String userName = ShiroUtils.getSysUser().getUserName();
        knowledgeClassify.setUpdateBy(userName);
        knowledgeClassify.setUpdateTime(DateUtil.date());
        knowledgeClassifyService.updateById(knowledgeClassify);
        return Result.success();
    }

    /**
     * 删除知识库类型
     */
    @RequiresPermissions("business:knowledgeClassify:remove")
    @Log(title = "知识库类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        knowledgeClassifyService.removeByIds(Arrays.asList(longIds));
        return Result.success();
    }

    /**
     * 导出知识库类型列表
     */
    @RequiresPermissions("business:knowledgeClassify:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(KnowledgeClassify knowledgeClassify) {
        List<KnowledgeClassify> list = knowledgeClassifyService.list(knowledgeClassify);
        ExcelUtil<KnowledgeClassify> util = new ExcelUtil<>(KnowledgeClassify.class);
        util.exportExcel(list, "knowledgeClassify");
        return Result.success();
    }
}
